package com.ts.api.http.platform.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.http.platform.entity.apply.ApiPlatApplyLiangZiQianBao;
import com.ts.api.http.platform.entity.dataexchange.ApiPlatDataExchangeLiangZiQianBao;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5Apply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;

import java.math.BigDecimal;
import java.util.*;

/**
 *  上游 安心分期（出）量子钱包 全28
 *
 *  @author WJB
 */
@ApiPlan(productId = "338", type = ApiPlanConvertType.PLAT_PHONE_MD5)
public class ApiPlatLiangZiQianBaoPhoneMd5Convert implements ApiPlatformPhoneMd5ConvertHandler {

    private static final String KEY = "qwerasdf12345678";
    private static final String CHANNEL_ID = "se3qcu";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        ApiPlatDataExchangeLiangZiQianBao data = new ApiPlatDataExchangeLiangZiQianBao();
        data.setMd5Phone(apiDataExchangeParameter.getPhoneMd5());
        data.setName("默认姓名");
        data.setInvite(CHANNEL_ID);
        //  年龄
        data.setAge(ClassConvertUtils.toInt(apiDataExchangeParameter.getAge()));
        //  性别：0 未知；1 男；2 女
        Integer sex = ChooseEq.create()
                .when(apiDataExchangeParameter.getSex(), "1", 2)
                .when(apiDataExchangeParameter.getSex(), "0", 1)
                .end(0);
        data.setSex(sex);
        data.setCity(apiDataExchangeParameter.getCity());
        data.setCityId(Integer.valueOf(apiDataExchangeParameter.getAreaCode()));
        //  ip
        data.setLoginIp(apiDataExchangeParameter.getIp());
        //  信用情况
        Integer overdue = ChooseEq.create()
                .when(apiDataExchangeParameter.getOverdue(), "1", 1)
                .when(apiDataExchangeParameter.getOverdue(), "2", 2)
                .when(apiDataExchangeParameter.getOverdue(), "3", 2)
                .end(0);
        data.setPeopleBankCredit(overdue);
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiDataExchangeParameter.getSesameCredit(), "1", 1)
                .when(apiDataExchangeParameter.getSesameCredit(), "2", 2)
                .when(apiDataExchangeParameter.getSesameCredit(), "3", 3)
                .when(apiDataExchangeParameter.getSesameCredit(), "4", 4)
                .end(0);
        data.setZhimaCredit(zhima);
        //  花呗
        Integer huaBei = ChooseEq.create()
                .when(apiDataExchangeParameter.getHbLimit(), "1", 0)
                .when(apiDataExchangeParameter.getHbLimit(), "2", 1)
                .when(apiDataExchangeParameter.getHbLimit(), "3", 2)
                .when(apiDataExchangeParameter.getHbLimit(), "4", 3)
                .when(apiDataExchangeParameter.getHbLimit(), "5", 4)
                .end(0);
        data.setHuabei(huaBei);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiDataExchangeParameter.getBtLimit(), "1", 0)
                .when(apiDataExchangeParameter.getBtLimit(), "2", 1)
                .when(apiDataExchangeParameter.getBtLimit(), "3", 2)
                .when(apiDataExchangeParameter.getBtLimit(), "4", 3)
                .when(apiDataExchangeParameter.getBtLimit(), "5", 4)
                .end(0);
        data.setBaitiao(btLimit);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiDataExchangeParameter.getSocialSecurity(), "1", 0)
                .when(apiDataExchangeParameter.getSocialSecurity(), "2", 3)
                .when(apiDataExchangeParameter.getSocialSecurity(), "3", 2)
                .when(apiDataExchangeParameter.getSocialSecurity(), "4", 1)
                .end(0);
        data.setHas(socialSecurity);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiDataExchangeParameter.getProvidentFund(), "1", 0)
                .when(apiDataExchangeParameter.getProvidentFund(), "2", 3)
                .when(apiDataExchangeParameter.getProvidentFund(), "3", 2)
                .when(apiDataExchangeParameter.getProvidentFund(), "4", 1)
                .end(0);
        data.setFund(providentFund);
        //  房产
        Integer house = ChooseEq.create()
                .when(apiDataExchangeParameter.getRealEstateInfo(), "1", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "2", 3)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "3", 3)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "4", 3)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "5", 3)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "6", 2)
                .end(0);
        data.setHouseProperty(house);
        //  车产
        Integer vehicle = ChooseEq.create()
                .when(apiDataExchangeParameter.getCarInfo(), "1", 0)
                .when(apiDataExchangeParameter.getCarInfo(), "2", 3)
                .when(apiDataExchangeParameter.getCarInfo(), "3", 3)
                .when(apiDataExchangeParameter.getCarInfo(), "4", 3)
                .end(0);
        data.setCarProperty(vehicle);
        //  商业保险
        Integer commericalInsurance = ChooseEq.create()
                .when(apiDataExchangeParameter.getInsurancePolicy(), "1", 0)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "2", 1)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "3", 1)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "4", 1)
                .end(0);
        data.setBdStatus(commericalInsurance);
        //  信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiDataExchangeParameter.getCreditLimit(), "1", 0)
                .when(apiDataExchangeParameter.getCreditLimit(), "2", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "3", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "4", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "5", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "6", 1)
                .end(0);
        data.setCreditCard(creditCard);
        data.setMicroLoan(0);
        //  营业执照
        Integer bizLicenseYear = ChooseEq.create()
                .when(apiDataExchangeParameter.getBizLicenseYear(), "1", 0)
                .when(apiDataExchangeParameter.getBizLicenseYear(), "2", 2)
                .when(apiDataExchangeParameter.getBizLicenseYear(), "3", 3)
                .when(apiDataExchangeParameter.getBizLicenseYear(), "4", 4)
                .end(0);
        data.setBusinessLicense(bizLicenseYear);
        //  贷款额度
        Integer money = ChooseEq.create()
                .when(apiDataExchangeParameter.getLoanAmount(), "1", 30000)
                .when(apiDataExchangeParameter.getLoanAmount(), "2", 50000)
                .when(apiDataExchangeParameter.getLoanAmount(), "3", 100000)
                .when(apiDataExchangeParameter.getLoanAmount(), "4", 200000)
                .end(30000);
        data.setLoanAmount(money);

        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));
        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelCode", CHANNEL_ID);
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        ApiPlatApplyLiangZiQianBao data = new ApiPlatApplyLiangZiQianBao();
        data.setTraceId(apiApplyParameter.getApplyId());
        data.setPhone(apiApplyParameter.getPhone());
        data.setName(apiApplyParameter.getUserName());
        data.setInvite(CHANNEL_ID);
        //  年龄
        data.setAge(ClassConvertUtils.toInt(apiApplyParameter.getAge()));
        //  性别：0 未知；1 男；2 女
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "1", 2)
                .when(apiApplyParameter.getSex(), "0", 1)
                .end(0);
        data.setSex(sex);
        data.setCity(apiApplyParameter.getCity());
        data.setCityId(Integer.valueOf(apiApplyParameter.getAreaCode()));
        //  ip
        data.setLoginIp(apiApplyParameter.getIp());
        //  信用情况
        Integer overdue = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", 1)
                .when(apiApplyParameter.getOverdue(), "2", 2)
                .when(apiApplyParameter.getOverdue(), "3", 2)
                .end(0);
        data.setPeopleBankCredit(overdue);
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 1)
                .when(apiApplyParameter.getSesameCredit(), "2", 2)
                .when(apiApplyParameter.getSesameCredit(), "3", 3)
                .when(apiApplyParameter.getSesameCredit(), "4", 4)
                .end(0);
        data.setZhimaCredit(zhima);
        //  花呗
        Integer huaBei = ChooseEq.create()
                .when(apiApplyParameter.getHbLimit(), "1", 0)
                .when(apiApplyParameter.getHbLimit(), "2", 1)
                .when(apiApplyParameter.getHbLimit(), "3", 2)
                .when(apiApplyParameter.getHbLimit(), "4", 3)
                .when(apiApplyParameter.getHbLimit(), "5", 4)
                .end(0);
        data.setHuabei(huaBei);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiApplyParameter.getBtLimit(), "1", 0)
                .when(apiApplyParameter.getBtLimit(), "2", 1)
                .when(apiApplyParameter.getBtLimit(), "3", 2)
                .when(apiApplyParameter.getBtLimit(), "4", 3)
                .when(apiApplyParameter.getBtLimit(), "5", 4)
                .end(0);
        data.setBaitiao(btLimit);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 0)
                .when(apiApplyParameter.getSocialSecurity(), "2", 3)
                .when(apiApplyParameter.getSocialSecurity(), "3", 2)
                .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                .end(0);
        data.setHas(socialSecurity);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 0)
                .when(apiApplyParameter.getProvidentFund(), "2", 3)
                .when(apiApplyParameter.getProvidentFund(), "3", 2)
                .when(apiApplyParameter.getProvidentFund(), "4", 1)
                .end(0);
        data.setFund(providentFund);
        //  房产
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 3)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 3)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 3)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 3)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 2)
                .end(0);
        data.setHouseProperty(house);
        //  车产
        Integer vehicle = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 3)
                .when(apiApplyParameter.getCarInfo(), "3", 3)
                .when(apiApplyParameter.getCarInfo(), "4", 3)
                .end(0);
        data.setCarProperty(vehicle);
        //  商业保险
        Integer commericalInsurance = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                .end(0);
        data.setBdStatus(commericalInsurance);
        //  信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 0)
                .when(apiApplyParameter.getCreditLimit(), "2", 1)
                .when(apiApplyParameter.getCreditLimit(), "3", 1)
                .when(apiApplyParameter.getCreditLimit(), "4", 1)
                .when(apiApplyParameter.getCreditLimit(), "5", 1)
                .when(apiApplyParameter.getCreditLimit(), "6", 1)
                .end(0);
        data.setCreditCard(creditCard);
        data.setMicroLoan(0);
        //  营业执照
        Integer bizLicenseYear = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", 0)
                .when(apiApplyParameter.getBizLicenseYear(), "2", 2)
                .when(apiApplyParameter.getBizLicenseYear(), "3", 3)
                .when(apiApplyParameter.getBizLicenseYear(), "4", 4)
                .end(0);
        data.setBusinessLicense(bizLicenseYear);
        //  贷款额度
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 30000)
                .when(apiApplyParameter.getLoanAmount(), "2", 50000)
                .when(apiApplyParameter.getLoanAmount(), "3", 100000)
                .when(apiApplyParameter.getLoanAmount(), "4", 200000)
                .end(30000);
        data.setLoanAmount(money);
        data.setLoanType(3);
        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));
        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelCode", CHANNEL_ID);
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespPlatformPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        return RespPlatformPhoneMd5Apply.ofSuc();
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        return null;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        return Map.of();
    }

    @Override
    public RespPlatformPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }

        JSONObject resData = jsonObject.getJSONObject("data");

        String orderId = resData.getString("traceId");
        BigDecimal price = resData.getBigDecimal("price");

        RespPlatformPhoneMd5DataExchange result = RespPlatformPhoneMd5DataExchange.ofSuc();

        result.setPrice(price);
        result.setApplyId(orderId);
        result.setDisplayName("量子钱包");
        result.setProductName("量子钱包");
        return result;
    }

}
